﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [CustomEditor(typeof(AudioImporter))]
    internal class AudioImporterInspector : AssetImporterInspector
    {
        private static string[] formatLabels = new string[2];
        private bool m_3D;
        private AudioType m_compressedType;
        private int m_compressionBitrate;
        private int m_durationMS;
        private bool m_ForceToMono;
        private AudioImporterFormat m_format;
        private bool m_Hardware;
        private bool m_iscompressible;
        private AudioImporterLoadType m_loadType;
        private bool m_Loopable;
        private int m_maxbitrate;
        private int m_minbitrate;
        private int m_origchannels;
        private AudioType m_type;

        internal override void Apply()
        {
            AudioImporter target = base.target as AudioImporter;
            target.format = this.m_format;
            target.loadType = this.m_loadType;
            target.compressionBitrate = this.m_compressionBitrate;
            target.threeD = this.m_3D;
            target.forceToMono = this.m_ForceToMono;
            target.hardware = this.m_Hardware;
            target.loopable = this.m_Loopable;
        }

        private int GetBitRateGUI(AudioImporter importer)
        {
            int num = (this.m_compressionBitrate >= 0) ? (this.m_compressionBitrate / 0x3e8) : (importer.defaultBitrate / 0x3e8);
            int num2 = EditorGUILayout.IntSlider("Compression (kbps)", num, this.m_minbitrate, this.m_maxbitrate, new GUILayoutOption[0]);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            if (this.m_format == AudioImporterFormat.Compressed)
            {
                float num3 = ((float) this.m_durationMS) / 1000f;
                int bytes = (int) (num3 * ((num * 1000f) / 4f));
                GUILayout.Label(string.Format("New file size : {0:000}", EditorUtility.FormatBytes(bytes)), new GUILayoutOption[0]);
            }
            GUILayout.EndHorizontal();
            return num2;
        }

        private int GetQualityGUI()
        {
            float num = (this.m_compressionBitrate >= 0) ? (((((float) this.m_compressionBitrate) / 1000f) - 56f) / 2f) : 50f;
            int num2 = (int) Math.Round((double) num);
            return EditorGUILayout.IntSlider("XMA Quality (1-100)", num2, 1, 100, new GUILayoutOption[0]);
        }

        internal override bool HasModified()
        {
            AudioImporter target = base.target as AudioImporter;
            return (((target.format != this.m_format) || (target.compressionBitrate != this.m_compressionBitrate)) || ((((target.loadType != this.m_loadType) || (target.threeD != this.m_3D)) || ((target.forceToMono != this.m_ForceToMono) || (target.hardware != this.m_Hardware))) || (target.loopable != this.m_Loopable)));
        }

        public override void OnInspectorGUI()
        {
            BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
            AudioImporter target = base.target as AudioImporter;
            if (target != null)
            {
                BuildTargetGroup group2 = buildTargetGroup;
                if (group2 != BuildTargetGroup.Wii)
                {
                    if (group2 != BuildTargetGroup.FlashPlayer)
                    {
                        GUI.enabled = ((this.m_type != AudioType.MPEG) && (this.m_type != AudioType.OGGVORBIS)) && this.m_iscompressible;
                        formatLabels[0] = "Native (" + this.m_type + ")";
                        formatLabels[1] = "Compressed (" + this.m_compressedType + ")";
                        int num3 = EditorGUILayout.Popup("Audio Format", ((int) this.m_format) + 1, formatLabels, new GUILayoutOption[0]);
                        this.m_format = (AudioImporterFormat) (num3 - 1);
                        GUI.enabled = true;
                        this.m_3D = EditorGUILayout.Toggle("3D Sound", this.m_3D, new GUILayoutOption[0]);
                        GUI.enabled = (this.m_origchannels > 1) && (target.origIsMonoForcable || ((this.m_format == AudioImporterFormat.Compressed) && this.m_iscompressible));
                        this.m_ForceToMono = EditorGUILayout.Toggle("Force to mono", this.m_ForceToMono, new GUILayoutOption[0]);
                        GUI.enabled = true;
                        string[] displayedOptions = null;
                        if (this.m_format == AudioImporterFormat.Compressed)
                        {
                            displayedOptions = new string[] { "Decompress on load", "Compressed in memory", "Stream from disc" };
                            this.m_loadType = (AudioImporterLoadType) EditorGUILayout.Popup("Load type", (int) this.m_loadType, displayedOptions, new GUILayoutOption[0]);
                        }
                        else
                        {
                            displayedOptions = new string[] { "Load into memory", "Stream from disc" };
                            int selectedIndex = Mathf.Clamp(((int) this.m_loadType) - 1, 0, 1);
                            EditorGUI.BeginChangeCheck();
                            selectedIndex = EditorGUILayout.Popup("Load type", selectedIndex, displayedOptions, new GUILayoutOption[0]);
                            if (EditorGUI.EndChangeCheck())
                            {
                                this.m_loadType = (AudioImporterLoadType) (selectedIndex + 1);
                            }
                        }
                        GUI.enabled = (buildTargetGroup == BuildTargetGroup.iPhone) && (this.m_format == AudioImporterFormat.Compressed);
                        this.m_Hardware = EditorGUILayout.Toggle("Hardware decoding", this.m_Hardware, new GUILayoutOption[0]);
                        GUI.enabled = true;
                        GUI.enabled = (this.m_iscompressible && ((this.m_compressedType == AudioType.MPEG) || (this.m_compressedType == AudioType.XMA))) && (this.m_format == AudioImporterFormat.Compressed);
                        this.m_Loopable = EditorGUILayout.Toggle(new GUIContent("Gapless looping", "Perform special MPEG encoding and stretch to loop the sound perfectly"), this.m_Loopable, new GUILayoutOption[0]);
                        GUI.enabled = true;
                        if (this.m_durationMS != 0)
                        {
                            int num5;
                            if (buildTargetGroup == BuildTargetGroup.XBOX360)
                            {
                                num5 = (0x38 + (2 * this.GetQualityGUI())) * 0x3e8;
                            }
                            else
                            {
                                num5 = this.GetBitRateGUI(target) * 0x3e8;
                            }
                            if (GUI.changed)
                            {
                                this.m_compressionBitrate = num5;
                            }
                        }
                        else
                        {
                            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                            GUILayout.Label("Unity failed to import this audio file. Try to reimport.", new GUILayoutOption[0]);
                            GUILayout.EndHorizontal();
                        }
                    }
                    else
                    {
                        this.m_3D = EditorGUILayout.Toggle("3D Sound", this.m_3D, new GUILayoutOption[0]);
                    }
                }
                else
                {
                    formatLabels[0] = "Native (" + this.m_type + ")";
                    formatLabels[1] = "Compressed (" + this.m_compressedType + ")";
                    int num = EditorGUILayout.Popup("Audio Format", ((int) this.m_format) + 1, formatLabels, new GUILayoutOption[0]);
                    this.m_format = (AudioImporterFormat) (num - 1);
                    this.m_3D = EditorGUILayout.Toggle("3D Sound", this.m_3D, new GUILayoutOption[0]);
                    if ((this.m_format != AudioImporterFormat.Compressed) && (this.m_origchannels > 1))
                    {
                        this.m_ForceToMono = EditorGUILayout.Toggle("Force to mono", this.m_ForceToMono, new GUILayoutOption[0]);
                    }
                    string[] strArray = null;
                    if (this.m_format == AudioImporterFormat.Compressed)
                    {
                        strArray = new string[] { "Compressed in memory", "Stream from disc" };
                    }
                    else
                    {
                        strArray = new string[] { "Decompress on load", "Compressed in memory", "Stream from disc" };
                    }
                    int num2 = (strArray.Length != 2) ? ((int) this.m_loadType) : (((int) this.m_loadType) - 1);
                    num2 = Mathf.Clamp(num2, 0, strArray.Length - 1);
                    num2 = EditorGUILayout.Popup("Load type", num2, strArray, new GUILayoutOption[0]);
                    this.m_loadType = (strArray.Length != 2) ? ((AudioImporterLoadType) num2) : ((AudioImporterLoadType) (num2 + 1));
                    if (this.m_format == AudioImporterFormat.Compressed)
                    {
                        this.m_Loopable = EditorGUILayout.Toggle(new GUIContent("Gapless looping", string.Empty), this.m_Loopable, new GUILayoutOption[0]);
                    }
                }
            }
            GUI.enabled = true;
            GUI.changed = false;
            base.ApplyRevertGUI();
            if (GUI.changed)
            {
                GUIUtility.ExitGUI();
            }
        }

        internal override void ResetValues()
        {
            AudioImporter target = base.target as AudioImporter;
            this.m_format = target.format;
            this.m_loadType = target.loadType;
            this.m_compressionBitrate = target.compressionBitrate;
            this.m_3D = target.threeD;
            this.m_ForceToMono = target.forceToMono;
            this.m_Hardware = target.hardware;
            this.m_Loopable = target.loopable;
            target.updateOrigData();
            this.m_durationMS = target.durationMS;
            this.m_origchannels = target.origChannelCount;
            this.m_iscompressible = target.origIsCompressible;
            this.m_type = target.origType;
            this.m_minbitrate = target.minBitrate(this.m_compressedType) / 0x3e8;
            this.m_maxbitrate = target.maxBitrate(this.m_compressedType) / 0x3e8;
            if (this.m_durationMS != 0)
            {
                this.m_compressedType = AudioUtil.GetPlatformConversionType(this.m_type, BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget), AudioImporterFormat.Compressed);
            }
        }
    }
}

